package com.lizhangjun.auth;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

/**
 * 安全配置
 * @author lizhangjun
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //开启跨域共享，跨域伪造请求限制=无效
        http.cors().and().csrf().disable();

        //开启授权认证
        http.authorizeRequests().anyRequest().permitAll();

        //访问限制
        http.exceptionHandling().accessDeniedHandler(new AuthLimitHandler());

        //登录过期/未登录 处理
        http.exceptionHandling().authenticationEntryPoint(new AccessDeniedHandler());

        //rest 无状态 无session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        //配置token验证过滤器
        //http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);

    }
}
